<?php
namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

trait AuthenticatesUsers {
    use RedirectsUsers, ThrottlesLogins;

    public function showLoginForm() {
        return view('auth.login');
    }

    public function login(Request $request) {
        $this->validateLogin($request); // 验证登录

        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request); // 锁定事件

            return $this->sendLockoutResponse($request);
        }

        if ( $this->attemptLogin($request) ) return $this->sendLoginResponse($request); // 登录成功

        $this->incrementLoginAttempts($request); // 增加尝试次数

        return $this->sendFailedLoginResponse($request); // 登录失败
    }
    protected function validateLogin(Request $request) {
        $this->validate($request, [
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }
    protected function attemptLogin(Request $request) {
        return $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
        );
    }
    protected function credentials(Request $request) {
        return $request->only($this->username(), 'password');
    }
    protected function sendLoginResponse(Request $request) {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());
    }
    protected function authenticated(Request $request, $user) {}
    protected function sendFailedLoginResponse(Request $request) {
        throw ValidationException::withMessages([
            $this->username() => [trans('auth.failed')],
        ]);
    }
    public function username() { // 登录标识
        return 'email';
    }
    public function logout(Request $request) {
        $this->guard()->logout();

        $request->session()->invalidate();

        return $this->loggedOut($request) ?: redirect('/');
    }
    protected function loggedOut(Request $request) {}
    protected function guard() {
        return Auth::guard();
    }
}
